﻿using System;
using System.IO;
using System.Web.UI.WebControls;

namespace WebApplication1.Admin
{
    public partial class Template : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtContents.Text = string.Empty;
                slt_template.Items.Clear();
                slt_template.Items.Add(new ListItem() { Text = "选择模板", Value = "" });

                var files = Directory.GetFiles(Server.MapPath("~/Admin/Templates"), "*.html", SearchOption.TopDirectoryOnly);
                foreach (var file in files)
                {
                    FileInfo info = new FileInfo(file);
                    slt_template.Items.Add(new ListItem() { Text = info.Name, Value = info.FullName });
                }
            }
        }

        protected void slt_template_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (slt_template.SelectedValue == "")
            {
                txtContents.Text = string.Empty;
                return;
            }
            txtContents.Text = File.ReadAllText(slt_template.SelectedValue, System.Text.Encoding.Default);
        }

        protected void btnSumbit_Click(object sender, EventArgs e)
        {
            if (slt_template.SelectedValue == "")
            {
                txtContents.Text = string.Empty;
                return;
            }
            string filePath = slt_template.SelectedValue;
            File.WriteAllText(filePath, txtContents.Text);
            ClientScript.RegisterStartupScript(this.GetType(), "key", "<script>$('#myModal').modal('show');</script>");
        }
    }
}